// increase the default animation speed to exaggerate the effect
$.fx.speeds._default = 1000;
$(function() {
	loadDialog("#tableAddress");
	search();
	evenClickTr();
	clearText();
	selectData();
	closeDialog();
	noneData();
	goToPage();
	enterToNextPage();
	loadPageSize();
});
function goToPage() {
	$("div.page a").click(function() {
		var postCode = $("#postCode").val();
		var street = $("#street").val();
		var town = $("#townName").val();
		var currPage = $(this).attr("title");
		var rePage = /^[\d]{1,4}$/;
		var pageSize = $("#pageSize").val();
		if (rePage.test(currPage) && currPage > 0) {
			loadDataAddress(postCode, street, town, currPage, pageSize);
		}
	});
}
function enterToNextPage() {
	$("#txtPage").keyup(function(e) {
		if (e.keyCode == 13) {
			var postCode = $("#postCode").val();
			var street = $("#street").val();
			var town = $("#townName").val();
			var currPage = $(this).val();
			var rePage = /^[\d]{1,4}$/;
			var pageSize = $("#pageSize").val();
			if (rePage.test(currPage) && currPage > 0)
				loadDataAddress(postCode, street, town, currPage,pageSize);
			else
				alert("Invalid page!");
			return false;
		}
	});
}
function loadDialog(idTable) {
	$(idTable).dialog({
		autoOpen : false,
		show : "blind",
		hide : "explode",
		width : "auto",
		title : "Post Code",
		modal : true
	});
	$("#showdialog").click(function() {
		$(idTable).dialog("open");
		return false;
	});
}

function evenClickTr() {
	$("div#tableAddress table tr").click(
			function() {
				$("div#tableAddress table tr").find("input[type=radio]").attr(
						"checked", "false");
				$(this).find("input[type=radio]").attr("checked", "true");
				$("div#tableAddress table tr").removeClass("rdbChecked");
				$(this).addClass("rdbChecked");
			});
}
function search() {
	$("#btnSearch").click(function() {
		var postCode = $("#postCode").val();
		var street = $("#street").val();
		var town = $("#townName").val();
		var pageSize = $("#pageSize").val();
		loadDataAddress(postCode, street, town, 1,pageSize);
	});
}
function loadPageSize(){
	$("#pageSize").change(function(){
		var postCode = $("#postCode").val();
		var street = $("#street").val();
		var town = $("#townName").val();
		var pageSize = $("#pageSize").val();
		var currPage = $("#txtPage").val();
		loadDataAddress(postCode, street, town, currPage,pageSize);
	});
}
function loadDataAddress(postCode, street, town, currPage, pageSize) {
	$.post('address.action', {
		'postCode' : postCode,
		'street' : street,
		'townName' : town,
		'currentPage' : currPage,		
		'pageSize':pageSize
	}, function(data) {
		$("#tableAddress").empty();
		$("#tableAddress").html(data);
	});
}
function clearText() {
	$("#btnClear").click(function() {
		$("#postCode").val("");
		$("#street").val("");
		$("#townName").val("");
	});
}
function selectData() {
	$("#btnSelect").click(function() {
		var postCode = "";
		$("div#tableAddress table tr td input[type=radio]").each(function() {
			if ($(this).is(":checked")) {
				postCode = $(this).val();
			}
		});
		if (postCode.length > 0) {
			$("input[name=postCodeVal]").val(postCode);
			$("div#tableAddress table tr").removeClass("rdbChecked");
			$("#tableAddress").dialog("close");
		}
	});
}
function closeDialog() {
	$("#btnClose").click(function() {
		$("#tableAddress").dialog("close");
	});
}
function noneData() {
	$("#btnNone").click(function() {
		$("input[name=postCodeVal]").val("");
		$("#tableAddress").dialog("close");
	});
}